<?php

namespace app\api\controller\test;

use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Linedevice;
use app\admin\model\Roomclean;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\DeviceService;
use app\api\service\RoomService;
use app\common\controller\Api;
use fast\Http;


class Lock extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    public function init(){

        $lockData = $this->request->post('lockData');
        $lockNo = $this->request->post('lockNo');
        $tokenUrl = "https://cnapi.sciener.com/oauth2/token";
        $tokenReq = array();
        $tokenReq['clientId'] = "4122b7859d9745a2bc32cdc084ca0231";
        $tokenReq['clientSecret'] = "07aa420424861bb837f58a33867b49d7";
        $tokenReq['username'] = "13348677754";
        $tokenReq['password'] = md5("johnwang1990");

        error_log("==req===".json_encode($tokenReq));
        $result = postCurl($tokenUrl,$tokenReq);
        $token = $result['access_token'];

        $initUrl = "https://cnapi.sciener.com/v3/lock/initialize";
        $initReq = array();
        $initReq['clientId'] = "4122b7859d9745a2bc32cdc084ca0231";
        $initReq['accessToken'] = $token;
        $initReq['lockData'] = $lockData;
        $initReq['date'] = getMillisecond();
        $initReq['lockAlias'] =$lockNo;

        $initResult = postCurl($initUrl,$initReq);

        error_log("====initResult===".json_encode($initResult));

        $this->success('',$initResult);
    }

     public function keyList(){

         $url = "https://cnapi.sciener.com/v3/lock/list";
         $tokenUrl = "https://cnapi.sciener.com/oauth2/token";
         $tokenReq = array();
         $tokenReq['clientId'] = "4122b7859d9745a2bc32cdc084ca0231";
         $tokenReq['clientSecret'] = "07aa420424861bb837f58a33867b49d7";
         $tokenReq['username'] = "13348677754";
         $tokenReq['password'] = md5("johnwang1990");

         error_log("==req===".json_encode($tokenReq));
         $result = postCurl($tokenUrl,$tokenReq);
         $token = $result['access_token'];
         error_log("==token===".$token);

         $keyReq = array();
         $keyReq['accessToken'] = $token;
         $keyReq['clientId'] = "4122b7859d9745a2bc32cdc084ca0231";
         $keyReq['pageNo'] = 1;
         $keyReq['pageSize'] = 1000;
         $keyReq['date'] = getMillisecond();
         $result = postCurl($url,$keyReq);

         $list = $result['list'];

         $lockDeviceModel = new Devicelock();
         foreach ($list as $key=>$val){
             $db = array();
             $db['lockAlias'] = $val['lockAlias'];
             $db['lockMac'] = $val['lockMac'];
             $db['lockId'] = $val['lockId'];
             $db['electricQuantity'] = $val['electricQuantity'];
             $db['lockData'] = $val['lockData'];

             $lock = $lockDeviceModel->where('lockId',$val['lockId'])->find();
             if(!$lock) {
                 $lockDeviceModel->insert($db);
             }else{
                 $lockDeviceModel->where('id',$lock['id'])->update($db);
             }

         }

         error_log("===key===".json_encode($result));
         $this->success('',$result);

     }

}
